package com.qjs.shiro.test.realm;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;

public class MapRealm implements Realm{
	
	private static Map<String,String> users;
	static{
		users=new HashMap<String,String>();
		users.put("zs", "123");
		users.put("lisi", "1234");
	}
	

	@Override
	public String getName() {
		return "mapRealm";
	}

	@Override
	public boolean supports(AuthenticationToken token) {
		return token instanceof UsernamePasswordToken;
	}

	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String name=(String) token.getPrincipal();
		String password=new String((char[])token.getCredentials());
		if(!users.containsKey(name)) throw new UnknownAccountException("用户名不存在");
		if(!password.equals(users.get(name))) throw new IncorrectCredentialsException("密码错误");
		AuthenticationInfo info=new SimpleAuthenticationInfo(name, password, getName());
		return info;
	}

}
